﻿using System;
using System.Linq;
using Fee.FileAttachs;
using Fee.IO.Exporters;
using Fee.Searchers;
using Fee.Stores;
using Fee.ViewModel;
using Identity.Stores;
using JwtWebApi.Data;
using JwtWebApi.Filters;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;

namespace JwtWebApi.Controllers.Manage
{
    [Authorize]
    [Route("manage/[controller]")]
    [ApiController]
    [EnableCors]
    public class VendorMatcherController : Controller
    {
        private readonly ApplicationDbContext _context;
        private readonly IDistrictStore<ApplicationDbContext> _districtStore;
        private readonly FileManager _fileManager;
        private readonly IVendorMatcherStore<ApplicationDbContext> _store;
        private readonly IVendorStore<ApplicationDbContext> _vendorStore;

        public VendorMatcherController(ApplicationDbContext context, IVendorMatcherStore<ApplicationDbContext> store,
            FileManager fileManager, IVendorStore<ApplicationDbContext> vendorStore,
            IDistrictStore<ApplicationDbContext> districtStore)
        {
            _context = context;
            _store = store;
            _fileManager = fileManager;
            _vendorStore = vendorStore;
            _districtStore = districtStore;
        }

        [HttpGet("list")]
        public IActionResult Index([FromQuery] VendorMatcherSearcher searcher)
        {
            var result = _store.List(searcher, out var total);
            return Json(new
            {
                data = result.Select(f => new VendorMatcherViewModel(f)),
                total
            });
        }

        [HttpPost("Save")]
        [ModelValidteFilter]
        public IActionResult Save([FromBody] VendorMatcherViewModel vendorDetailViewModel)
        {
            var vendor = vendorDetailViewModel.ToVendorMatcher(_vendorStore, _districtStore, _store);

            _context.Update(vendor);

            _context.SaveChanges();
            return Json(new {successed = true, message = "成功"});
        }

        [HttpGet("get/{id}")]
        public IActionResult Get(int id)
        {
            var district = _context.VendorMatchers
                .Include(f => f.District)
                .Include(f => f.Vendor)
                .FirstOrDefault(_ => _.Id == id);
            return Json(district);
        }

        [HttpGet("export")]
        public IActionResult Export([FromQuery] VendorMatcherSearcher searcher)
        {
            var result = _store.List(searcher, out var total);

            var vendor = new VendorMatcherExportor();
            var fileForWrite = _fileManager.GetTempFile();
            vendor.ToExcel(System.IO.File.OpenWrite(fileForWrite), result);

            var contentType = "application/octet-stream";
            var fileName = "供应商映射" + DateTime.Now.ToString("yyyyMMdd hh:mm:ss") + "数据共" + total + ".xlsx";

            return File(System.IO.File.OpenRead(fileForWrite), contentType, fileName);
            ;
        }
    }
}